package cn.tedu.egweibo5.controller;

import cn.tedu.egweibo5.base.response.JsonResult;
import cn.tedu.egweibo5.mappers.CommentMapper;
import cn.tedu.egweibo5.pojo.dto.CommentDTO;
import cn.tedu.egweibo5.pojo.entity.Comment;
import cn.tedu.egweibo5.pojo.vo.CommentListVO;
import cn.tedu.egweibo5.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: Miyako
 * @CreateTime: 2024-08-14  22:33
 * @Description: TODO
 */
@Slf4j
@Api(tags = "评论模块")
@RestController
@RequestMapping("/v1/comment/")
@Validated
public class CommentController {
    @Autowired
    CommentMapper commentMapper;

    @ApiOperation("发布评论")
    @PostMapping("insert")
    public JsonResult insert(@Validated @RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session){
        UserVO user = (UserVO) session.getAttribute("user");
        Comment comment = new Comment();
        comment.setUserId(user.getId());
        BeanUtils.copyProperties(commentDTO,comment);
        commentMapper.insert(comment);
        return JsonResult.ok(null);
    }


    @ApiOperation("获取评论列表")
    @GetMapping("selectByWeiboId")
    @ApiImplicitParam(name = "id" , value = "微博id", required = true , paramType = "long")
    public JsonResult selectByWeiboId(@Range(min = 200,max = 300,message = "微博id范围200-300")@RequestParam Long id){
        List<CommentListVO> commentListVOS = commentMapper.selectByWeiboId(id);
        return JsonResult.ok(commentListVOS);

    }

}
